package com.example.lanouhn.firstapp.activity.fragment;

import android.app.Activity;
import android.app.FragmentManager;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.example.lanouhn.firstapp.R;
import com.example.lanouhn.firstapp.activity.activity.MainActivity;
import com.example.lanouhn.firstapp.activity.activity.NappyActivity;
import com.example.lanouhn.firstapp.activity.activity.NotifyActivity;
import com.example.lanouhn.firstapp.activity.adapter.ClassifyAdapter;
import com.example.lanouhn.firstapp.activity.constants.Constants;
import com.example.lanouhn.firstapp.activity.model.Classify;
import com.example.lanouhn.firstapp.activity.utils.ClassifyJson;
import com.example.lanouhn.firstapp.activity.utils.HttpUtils;
import com.example.lanouhn.firstapp.activity.view.xlistview.XListView;

import java.util.List;

public class ClassifyFragment extends Fragment {
    //设置变量
    private XListView listView;
    private ClassifyAdapter adapter;
    //定义数组
    private List<Classify> dataList;
    //加载进度
    private ProgressBar progressBar;

    //调用系统返回键实现点击返回事件

    private ImageView notify;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_fragment_classify, null);
        //找控件
        listView = (XListView) view.findViewById(R.id.classify_listView);
        progressBar = (ProgressBar) view.findViewById(R.id.progressBar);

        notify = (ImageView) view.findViewById(R.id.notify);

        //开启加载更多功能 设置上拉加载的开关 false为关闭  不显示
        listView.setPullLoadEnable(false);

        //设置listview中的item的点击事件
        onItemClickListener();
        return view;
    }

    private void onItemClickListener() {
        //点击消息通知按钮
        notify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), NotifyActivity.class);
                startActivity(intent);
                getActivity().overridePendingTransition(R.anim.translatein,R.anim.translateout);
            }
        });

        //实现listView的刷新加载监听
        listView.setXListViewListener(new XListView.IXListViewListener() {
            @Override
            public void onRefresh() {
                //请求网络数据
                getDataFromWeb();
            }

            @Override
            public void onLoadMore() {

            }
        });
                /**
                 * 分类列表的点击解析事件
                 */
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = null;
                intent = new Intent();
                intent.setClass(getActivity(), NappyActivity.class);
                switch (i) {

                    case 1://纸尿裤的解析

                        //携带参数跳转 用来在Activity中获取不同的URL地址
                        intent.putExtra("type", 0);
                        startActivity(intent);
                        break;
                    case 2:  //成长辅助的解析
                        intent.putExtra("type", 1);
                        startActivity(intent);
                        break;
                    case 3: //奶粉的解析
                        intent.putExtra("type", 2);
                        startActivity(intent);
                        break;
                    case 4: //婴幼儿洗护解析
                        intent.putExtra("type", 3);
                        startActivity(intent);
                        break;
                    case 5:  //辅食营养的解析
                        intent.putExtra("type", 4);
                        startActivity(intent);
                        break;
                    case 6:  //喂养的解析
                        intent.putExtra("type", 5);
                        startActivity(intent);
                        break;
                    case 7:  //女性专区的解析
                        intent.putExtra("type", 6);
                        startActivity(intent);
                        break;
                    case 8:  //儿童玩具的解析
                        intent.putExtra("type", 7);
                        startActivity(intent);
                        break;
                    case 9:  //食品保健的解析
                        intent.putExtra("type", 8);
                        startActivity(intent);
                        break;
                    case 10:  //家居日用的解析
                        intent.putExtra("type", 9);
                        startActivity(intent);
                        break;
                    case 11:  //童车的解析
                        intent.putExtra("type", 10);
                        startActivity(intent);
                        break;
                    case 12:  //婴儿服饰的解析
                        intent.putExtra("type", 11);
                        startActivity(intent);
                        break;
                    case 13:  //童装的解析
                        intent.putExtra("type", 12);
                        startActivity(intent);
                        break;
                    case 14:
                        intent.putExtra("type", 13);
                        startActivity(intent);
                        break;



                }
            }
        });

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);



        //请求网络数据
        getDataFromWeb();


    }

    /**
     * 请求网路数据，开启新线程
     */
    private void getDataFromWeb() {
        //显示进度条
        progressBar.setVisibility(View.VISIBLE);
        new Thread(new Runnable() {
            @Override
            public void run() {
                //调用HttpUtils类,来请求网络获取Json数据
                String result = HttpUtils.doGet(Constants.CLASSIFY_URLS);
                //将获取到的Json数据解析,通过封装的工具类
                dataList = ClassifyJson.getClassify(result);
                //判断解析出的集合长度,有数据就显示,无数据就不显示
                if (dataList.size() > 0)
                    handler.sendEmptyMessage(1);
                else
                    handler.sendEmptyMessage(0);
            }
        }).start();
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            //取消loading  进度条
            progressBar.setVisibility(View.GONE);
            switch (msg.what) {
                case 0:
                    Toast.makeText(getActivity(),
                            "请求失败,请稍候再试!", Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    //获取数据成功,并解析数据成功,创建adapter
                    adapter = new ClassifyAdapter(dataList, getActivity());
                    //设置adapter
                    listView.setAdapter(adapter);
                    //关闭下拉刷新
                    listView.stopRefresh();
                    break;
            }
        }
    };

}
